﻿using System;
using System.Windows;

namespace CCSManager
{
    /// <summary>
    /// Interaction logic for Main.xaml
    /// </summary>
    public partial class Main : Window
    {
        private bool cancellingEditProduct;
        private bool updatingProduct;

        public Main()
        {
            InitializeComponent();
        }

        private void LoginControl_LoggedIn(object sender, EventArgs e)
        {
            var ccsWebService = new CCS.CCS();
            App.Credentials = new CCS.CCSCredentials { UserName = LoginControl.UserName, Password = LoginControl.Password };
            if (ccsWebService.ValidateUser(App.Credentials))
            {
                Guid providerUserKey = ccsWebService.GetProviderUserKey(LoginControl.UserName, App.Credentials);
                App.UserKey = providerUserKey;
                AuthTabs.Visibility = Visibility.Hidden;
                ProductsControl.LogoutRequested += new EventHandler(ProductsControl_LogoutRequested);
                ProductsControl.Visibility = Visibility.Visible;
                ProductsControl.Refresh();
            }
        }

        void ProductsControl_LogoutRequested(object sender, EventArgs e)
        {
            App.DeveloperToken = Guid.Empty;
            App.UserKey = Guid.Empty;
            ProductsControl.Visibility = Visibility.Hidden;
            AuthTabs.Visibility = Visibility.Visible;
            ProductsControl.Clear();
            LoginControl.userNameTextBox.Text = string.Empty;
            LoginControl.passwordTextBox.Password = string.Empty;
            RegistrationCompleteMessage.Visibility = Visibility.Hidden;
        }

        private void RegisterControl_Registered(object sender, EventArgs e)
        {
            registerTabItem.Visibility = Visibility.Hidden;
            AuthTabs.SelectedIndex = 0;
            RegistrationCompleteMessage.Visibility = Visibility.Visible;
        }

        private void ProductsControl_ProductSelected(object sender, ProductSelectedEventArgs e)
        {
            if (!cancellingEditProduct && !updatingProduct)
            {
                var ccsWebService = new CCS.CCS();
                ProductsControl.Visibility = Visibility.Hidden;
                ProductControl.Application = e.Application;
                ProductControl.Refresh();
                ProductControl.Visibility = Visibility.Visible;
            }
        }

        private void ProductControl_EditCancelled(object sender, EventArgs e)
        {
            cancellingEditProduct = true;
            _CloseProductDetailAndRefresh();
            cancellingEditProduct = false;
        }

        private void _CloseProductDetailAndRefresh()
        {
            ProductControl.Visibility = Visibility.Hidden;
            ProductsControl.Visibility = Visibility.Visible;
            ProductsControl.Refresh();
            ProductsControl.productDataGrid.SelectedIndex = -1;
        }

        private void ProductControl_ProductDeleteRequested(object sender, ProductDeleteRequestedEventArgs e)
        {
            if (MessageBoxResult.No == MessageBox.Show("Really delete application?", "Delete Application", MessageBoxButton.YesNo))
            {
                return;
            }

            var ccsWebService = new CCS.CCS();
            if (ccsWebService.DeleteProduct(e.Application.ID, App.Credentials))
            {
                ProductsControl.Visibility = Visibility.Visible;
                ProductsControl.Refresh();
                ProductControl.Close();
            }
        }

        private void ProductControl_ProductUpdateRequested(object sender, ProductUpdateRequestedEventArgs e)
        {
            updatingProduct = true;
            var ccsWebService = new CCS.CCS();
            bool updated = ccsWebService.UpdateProduct(App.Credentials, e.Application);
            if (updated)
            {
                _CloseProductDetailAndRefresh();
            }
            updatingProduct = false;
        }
    }
}